package com.zsyh.changshang.adress;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.google.gson.Gson;
import com.zsyh.changshang.R;
import com.zsyh.changshang.basic.MyAppliaction;
import com.zsyh.changshang.bean.PrivicesBean;
import com.zsyh.changshang.data.DataApi;
import com.zsyh.changshang.util.EmptyUtil;
import com.zsyh.changshang.util.SPUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zzq on 16/9/26.
 */
public class ProvinceFragment implements AdapterView.OnItemClickListener{
    private AddressCallBack callBack;
    private String code;
    private Context context;
    private AddressAdapter adapter;
    private ListView listview;
    private  List<AddressManager.Province> list=new ArrayList<>();
    private List<String> shengfenlist;
    private List<PrivicesBean.DataBean> cityselist;
    private DataApi dataApi;

    public ProvinceFragment(Context context, AddressCallBack callBack){
        this.context = context;
        this.callBack = callBack;
        initView();
    }
    public void setCode(String code){
        this.code = code;
    }

    public ListView getListview() {
        return listview;
    }

    public View initView() {
        listview = (ListView) LayoutInflater.from(context).inflate(R.layout.select_address_pop_listview,null);
        String data = (String) SPUtils.get(MyAppliaction.getContext(), "shenb", "");
        Log.e("datashen",data+"空的");
        Gson gson = new Gson();
        PrivicesBean privicesBean=gson.fromJson(data,PrivicesBean.class);
        if(EmptyUtil.isEmpty(privicesBean.getData())){

        }else{
            for(int i = 0; i<privicesBean.getData().size(); i++){
                String province=privicesBean.getData().get(i).getAreaName()+"";
                String code=privicesBean.getData().get(i).getId()+"";
                AddressManager.Province  sheng=new AddressManager.Province(province,code);
                list.add(sheng);
            }
        }


        //adapter = new AddressAdapter(AddressManager.newInstance().getAllProvinces());
        adapter=new AddressAdapter(list);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);
        return listview;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //code = AddressManager.newInstance().getAllProvinces().get(i).getCode();
        code = list.get(i).getCode();
        Log.e("codef",code);
        if (callBack != null){
            callBack.selectProvince(list.get(i));
        }
        adapter.notifyDataSetChanged();
    }

    class AddressAdapter extends BaseAdapter {

        private List<AddressManager.Province> list;

        public AddressAdapter(List<AddressManager.Province> list){
            this.list = list;
        }

        @Override
        public int getCount() {
            if (list != null) {
                return list.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(context).inflate(R.layout.address_listiew_item_textview, null);
            TextView text = (TextView) view.findViewById(R.id.tvTextName);
            ImageView ivSelect = (ImageView) view.findViewById(R.id.ivSelect);
            text.setText(list.get(i).getName());
            Log.e("shenfen",list.get(0).getCode());
            if (list.get(i).getName().equals(code)) {
                text.setTextColor(context.getResources().getColor(R.color.text_button));
                ivSelect.setVisibility(View.VISIBLE);
            }
            return view;
        }
    }


}
